<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;

use think\Controller;
use think\Request;

/**
 * @icon users
 */
class User extends Backend
{

    /**
     * Users模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Users');

    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where = array();
            $filter=input();
            $filters = json_decode($filter['filter'], true);
            if(!empty($filters['user_id'])){
                $where['user_id']=$filters['user_id'];
            }
            if(!empty($filters['user_name'])){
                $where['user_name']=$filters['user_name'];
            }
            if(!empty($filters['nickname'])){
                $where['full_name or nickname']=array('like','%'.$filters['nickname'].'%');
            }
            if(!empty($filters['reg_times'])) {
                $date = explode(',', $filters['reg_times']);
                foreach($date as &$v){
                    $v = strtotime($v);
                }
                $where['reg_time'] = array('between', $date);
            }
            if(!empty($filters['reg_step'])){
                $where['reg_step'] = $filters['reg_step'];
            }
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach($list as &$v) {
                $v['sex'] = $v['sex']==1?'男':'女';
                if($v['reg_step']==0){
                    $v['reg_step']='一般';
                }elseif($v['reg_step']==1){
                    $v['reg_step']='高级';
                }elseif($v['reg_step']==2){
                    $v['reg_step']='认证';
                }else{
                    $v['reg_step']='错误数据';
                }
                $v['reg_times']=date('Y-m-d',$v['reg_time']);
            }
            unset($v);

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        $btns = ['refresh'];

        $this->view->assign('btns', $btns);
        return $this->view->fetch();
    }
    /**
     * 详情
     */
    public function detail($ids)
    {
        $row = $this->model
            ->get(['user_id' => $ids]);
        if (!$row) {
            $this->error('查询信息不存在');
        }

        $row = $row->toArray();
        $row['id_number_img'] = explode('-', $row['id_number_img']);
        $row['other_photo'] = explode('-', $row['other_photo']);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds))
            {
                $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();
            $count = 0;
            foreach ($list as $k => $v)
            {
                $count += $v->delete();
            }
            if ($count)
            {
                $this->success();
            }
            else
            {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds))
        {
            if (!in_array($row[$this->dataLimitField], $adminIds))
            {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false)
                    {
                        $this->success();
                    }
                    else
                    {
                        $this->error($row->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);

        return $this->view->fetch();
    }
}
